package com.my.mymafengwo.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.smallstrong.mafengwopro.R;
import com.lidroid.xutils.BitmapUtils;
import com.my.mymafengwo.bean.Local;
import com.my.mymafengwo.utils.ConstantUtils;

import java.util.List;

/**
 * ***********************************************
 * author:wgh
 * time:2015/12/4 22:13
 * copyright:
 * name:
 * step:
 * ***********************************************
 */
public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.RecyclerViewHolder> {

    private Context context;
    private List<Local.DataEntity.ProductsEntity.ListEntity> data;

    public RecyclerAdapter(Context context, List<Local.DataEntity.ProductsEntity.ListEntity> data) {
        this.context = context;
        this.data = data;
    }

    @Override
    public RecyclerViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_list_local_product, parent, false);
        RecyclerViewHolder viewHolder = new RecyclerViewHolder(view);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(final RecyclerViewHolder holder, final int position) {
        holder.tvCity.setText(data.get(position).getTag());
        holder.tvType.setText(data.get(position).getType());
        holder.tvTitle.setText(data.get(position).getTitle());
        holder.tvPrice.setText(data.get(position).getPrice_current() + "");
        holder.tvDiscount.setText(data.get(position).getDiscount());
        loadImage(holder.ivIcon, data.get(position).getThumbnail());

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mListener != null) {
                    mListener.onItemClick(holder.itemView, position);
                }
            }
        });
    }

    private void loadImage(ImageView iv, String url) {
        BitmapUtils bitmap = new BitmapUtils(context);
        bitmap.configDiskCacheEnabled(true);
        bitmap.configMemoryCacheEnabled(false);
        bitmap.configDefaultCacheExpiry(ConstantUtils.CACHEEXPIRY);
        bitmap.display(iv, url);
    }

    @Override
    public int getItemCount() {
        return data == null ? 0 : data.size();
    }

    public class RecyclerViewHolder extends RecyclerView.ViewHolder {

        public TextView tvCity, tvType, tvTitle, tvPrice, tvDiscount;
        public ImageView ivIcon;

        public RecyclerViewHolder(View itemView) {
            super(itemView);
            tvCity = (TextView) itemView.findViewById(R.id.tv_cityname);
            tvType = (TextView) itemView.findViewById(R.id.tv_type);
            tvTitle = (TextView) itemView.findViewById(R.id.tv_title);
            tvPrice = (TextView) itemView.findViewById(R.id.tv_price);
            tvDiscount = (TextView) itemView.findViewById(R.id.tv_discount);
            ivIcon = (ImageView) itemView.findViewById(R.id.iv_icon);
        }
    }

    public interface OnItemClickListener {
        void onItemClick(View view, int position);
    }

    /**
     * 声明每项的接口
     */
    private OnItemClickListener mListener;

    public void setOnItemClickListener(OnItemClickListener listener) {
        mListener = listener;
    }
}
